#include<bits/stdc++.h>
#define re return
#define rep(i,n) for(int i=0;i<n;i++)
#define ll long long
// #define int ll
#define forn(i,a,n) for(int i=a;i<n;i++)
#define sz(s) (int)(s.size())
#define pii pair<int,int>
#define fr first
#define se second
using namespace std;
int n,m,k;
const int maxn=4e5;
int base_a[maxn],a[maxn],c[maxn];
int X[4];
int s[maxn];
int dp[maxn][19];
int prmx[maxn][19];
int nd[maxn];
ll res[19][maxn];
int ndep[maxn];
int mxr[19];
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    cin>>n>>m;
    while((1<<k)<n)
    k++;
    rep(i,n)
    cin>>base_a[i];
    rep(i,m)
    cin>>c[i];
    rep(i,m)
    c[i]--;
    rep(i,m){
        int x=c[i]+(1<<k);
        ndep[i]=k;
        while(1){
            if(x==(1<<ndep[i]))
            break;
            ndep[i]--;
            x>>=1;
        }
    }
    rep(i,k+1){
        mxr[i]=(1<<(k-i))-1;
    }
    for(int dep=k-1;dep>=0;dep--){
        string now;
        cin>>now;
        rep(i,(1<<dep)){
            nd[(1<<dep)|i]=k-dep;
            s[(1<<dep)|i]=now[i]-'0';
        }
    }
    int t;
    cin>>t;
    while(t--){
        rep(i,4)
        cin>>X[i];
        rep(i,k+1){
            rep(j,mxr[i]+1)
            res[i][j]=0;
        }
        rep(i,n){
            int id=i+(1<<k);
            a[id]=(base_a[i]^X[(i+1)%4]);
            if(a[id]>k)
            a[id]=k;
        }
        // cout<<"\n";
        // cout<<clock()<<"\n";
        forn(i,(1<<k),(1<<(k+1))){
            int ni=i-(1<<k);
            if(i-(1<<k)>=n)
            a[i]=0;
            dp[i][a[i]]=n-1;
            rep(j,k+1){
                if(j!=a[i]){
                    if(ni<n)
                    dp[i][j]=ni-1;
                    else
                    dp[i][j]=n-1;    
                }
            }
            rep(j,a[i])
            prmx[i][j]=ni-1;
            forn(j,a[i],k+1)
            prmx[i][j]=n-1;
        }
        // cout<<clock()<<"\n";
        for(int i=(1<<k)-1;i>=1;i--){
            int mx=-1;
            int hs=(i<<1|s[i]),ls=(i<<1|(s[i]^1));
            mx=prmx[hs][nd[i]-1];
            int now=-1;
            rep(j,k+1){
                int nv=mx;
                if(dp[ls][j]<nv)
                nv=dp[ls][j];
                if(j<nd[i]){
                    dp[i][j]=nv;
                }else{
                    if(dp[hs][j]>nv)
                    dp[i][j]=dp[hs][j];
                    else
                    dp[i][j]=nv;
                }
                if(dp[i][j]>now)
                now=dp[i][j];
                prmx[i][j]=now;
            }
        }
        // cout<<clock()<<"\n";
        forn(i,(1<<k),(1<<(k+1))){
            int x=i,dep=k;
            int now=n-1;
            int ni=i-(1<<k)-1,idi=i-(1<<k)+1;
            while(x){
                if(now<0)
                break;
                if(x==(1<<dep)){
                    if(now<=mxr[dep])
                    res[dep][now]+=idi;
                    else
                    res[dep][mxr[dep]]+=idi;
                }
                if(x==1)
                break;
                if((x&1)==s[x>>1]){
                    if(a[i]>=nd[x>>1]){
                        x>>=1;
                        dep--;
                        continue;
                    }
                    if(now>ni)
                    now=ni;
                    x>>=1;
                    dep--;
                    continue;
                }
                x>>=1;
                int hs=(x<<1|s[x]);
                // if(t==3){
                //     cout<<x<<" "<<hs<<" "<<nd[x]<<" "<<dp[hs][nd[x]-2]<<"\n";
                // }
                int nt=prmx[hs][nd[x]-1];
                if(now>nt)
                now=nt;
                dep--;
            }
        }
        rep(i,k+1){
            for(int j=mxr[i]-1;j>=0;j--)
            res[i][j]+=res[i][j+1];
        }
        ll ans=0;
        rep(i,m){
            ans^=(res[ndep[i]][c[i]]*(i+1));
        }
        cout<<ans<<"\n";
    }
    re 0;
}